<template>
  <!-- 发端数据详情 -->
  <div class="start-data-container">
    <!-- 审核/编辑/详情 对话框 -->
    <el-dialog :title="title" v-model="dialogVisiable" :width="`${dynamic.dialogWidth}`" append-to-body
      :close-on-press-escape="preview_image_mask ? false : true">
      <!-- 图片显示区域 -->
      <div :style="displayStatus" class="dy-dialog">
        <div class="demo-image__lazy" :style="backgroundStyle">
          <el-image style="width: 100%;height: 100%; border-radius: 4px" :src="dialogPreviewUrl"
            :preview-src-list="[dialogPreviewUrl]" hide-on-click-modal preview-teleported
            @close="preview_image_mask = false" @show="preview_image_mask = true">
            <template #error>
              <div class="image-slot" style="font-size: 20px">
                未上传
              </div>
            </template>
          </el-image>
        </div>

        <!-- 详情表单区域 -->
        <div class="dy-form">
          <el-form class="form" :style="formStyle" label-width="100px">
            <el-form-item label="车牌号：" prop="plateNo" class="form-items" :style="`width: ${100 / formColumn}%`">
              <span class="model-value" v-if="formView.plateNo">{{ formView.plateNo
                }}</span>
              <span class="model-value no-model-value" v-else>{{ '-' }}</span>
            </el-form-item>
            <el-form-item label="司机：" prop="driverName" class="form-items" :style="`width: ${100 / formColumn}%`">
              <span class="model-value" v-if="formView.driverName">{{ formView.driverName
                }}</span>
              <span class="model-value no-model-value" v-else>{{ '-' }}</span>
            </el-form-item>
            <el-form-item label="线路：" prop="routeName" class="form-items" :style="`width: ${100 / formColumn}%`">
              <span class="model-value" v-if="formView.routeName">{{ formView.routeName
                }}</span>
              <span class="model-value no-model-value" v-else>{{ '-' }}</span>
            </el-form-item>
            <el-form-item label="货物：" prop="cargo" class="form-items" :style="`width: ${100 / formColumn}%`">
              <span class="model-value" v-if="formView.cargo">{{ formView.cargo }}</span>
              <span class="model-value no-model-value" v-else>{{ '-' }}</span>
            </el-form-item>
            <el-form-item label="发车时间：" prop="departureTime" class="form-items" :style="`width: ${100 / formColumn}%`">
              <span class="model-value" v-if="formView.departureTime">{{
                formView.departureTime
              }}</span>
              <span class="model-value no-model-value" v-else>{{ '-' }}</span>
            </el-form-item>
            <el-form-item label="到达时间：" prop="arrivalTime" class="form-items" :style="`width: ${100 / formColumn}%`">
              <span class="model-value" v-if="formView.arrivalTime">{{
                formView.arrivalTime
              }}</span>
              <span class="model-value no-model-value" v-else>{{ '-' }}</span>
            </el-form-item>
            <el-form-item label="磅单号：" prop="weightBillNo" class="form-items" :style="`width: ${100 / formColumn}%`">
              <span class="model-value" v-if="formView.weightBillNo">{{
                formView.weightBillNo
              }}</span>
              <span class="model-value no-model-value" v-else>{{ '-' }}</span>
            </el-form-item>
            <el-form-item label="净重：" prop="netWeight" class="form-items" :style="`width: ${100 / formColumn}%`">
              <span class="model-value" v-if="formView.netWeight">{{ formView.netWeight + '吨'
                }}</span>
              <span class="model-value no-model-value" v-else>{{ '-' }}</span>
            </el-form-item>
            <el-form-item label="箱号：" prop="containerNo" class="form-items" :style="`width: ${100 / formColumn}%`">
              <span class="model-value" v-if="formView.containerNo">{{
                formView.containerNo
              }}</span>
              <span class="model-value no-model-value" v-else>{{ '-' }}</span>
            </el-form-item>
            <el-form-item label="审核状态：" prop="checked" class="form-items" :style="`width: ${100 / formColumn}%`">
              <span class="model-value" v-if="formView.checked === 1">{{ '已审核'
                }}</span>
              <span class="model-value no-model-value" v-else>{{ '未审核' }}</span>
            </el-form-item>
            <el-form-item label="审核人：" prop="checkNotes" class="form-items" :style="`width: ${100 / formColumn}%`">
              <span class="model-value" v-if="formView.checkBy">{{ formView.checkBy
                }}</span>
              <span class="model-value no-model-value" v-else>{{ '-' }}</span>
            </el-form-item>
            <el-form-item label="审核时间：" prop="checkTime" class="form-items" :style="`width: ${100 / formColumn}%`">
              <span class="model-value" v-if="formView.checkTime">{{ formView.checkTime
                }}</span>
              <span class="model-value no-model-value" v-else>{{ '-' }}</span>
            </el-form-item>
            <el-form-item label="备注：" prop="checkNotes" class="form-items" style="width: 100%">
              <span class="model-value model-remark" v-if="formView.checkNotes">{{ formView.checkNotes
                }}</span>
              <span class="model-value no-model-value" v-else>{{ '-' }}</span>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, toRefs, onBeforeMount, onMounted, watchEffect, computed, getCurrentInstance } from 'vue';
import { transportInfoApi } from "@/api/transportOrderManagement/transportOrderList";
import { useRoute, useRouter } from 'vue-router';
const { proxy } = getCurrentInstance();
const route = useRoute();
let title = ref('查看运单')
let preview_image_mask = ref(false)
let dialogVisiable = ref(false);
/** 审核、详情、编辑对话框图片 */
let dialogPreviewUrl = ref('');
/** 审核、详情、编辑对话框图片背景展示状态样式 */
let backgroundStyle = computed(() => {
  return {
    width: dynamic.dynamicWidth == 0 ? '100%' : dynamic.dynamicWidth + 'px', // 根据需要设置宽度
    height: dynamic.dynamicHeight + 'px', // 根据需要设置高度
  };
})
/** 审核、详情、编辑对话框动态背景 */
let dynamic = reactive({
  dialogWidth: 1000,
  dynamicBack: '',
  dynamicWidth: 0,
  dynamicHeight: 0
})
/** 审核、详情、编辑对话框展示样式 */
let displayStatus = reactive({
  display: 'flex',
  flexDirection: '',
})
/** 审核、详情、编辑对话框表单的展示样式 */
let formStyle = reactive({
  display: 'flex',
  flexWrap: 'wrap',
  height: 'max-content',
  width: '',
})
/** 审核、详情、编辑对话框表单的展示列数 */
let formColumn = ref(0)
const data = reactive({
  formView: {}
});
const { formView } = toRefs(data);
onBeforeMount(() => {
})
onMounted(() => {})
watchEffect(() => {
})
/**
 *  Method
 * 
 */
/** 处理图片回显dialog动态样式 */
function getDyStyle(row) {
  return new Promise((resolve, reject) => {
    getImageSize(`${row.weightBillImgUrl}`, function (size) {
      if (size) {
        const { width, height } = size
        const ImageH = Number(height)
        const ImageW = Number(width)
        getCalcSize(ImageW, ImageH)
        getUnitByColumn(ImageW, ImageH)
      } else {
        const ImageH = Number(1024)
        const ImageW = Number(768)
        getCalcSize(ImageW, ImageH)
        getUnitByColumn(ImageW, ImageH)
      }
      resolve()
    });
  })
}

/** 获取图片尺寸 */
function getImageSize(url, callback) {
  var img = new Image();
  img.onload = function () {
    callback({ width: img.width, height: img.height });
  };
  img.onerror = function () {
    callback(null);
  };
  img.src = url;
}

/** 对话框每列展示formItem个数 */
function getUnitByColumn(w, h) {
  const dynamicW = dynamic.dialogWidth - 60
  if (w > h) {
    formColumn.value = 3
    formStyle.width = dynamicW + 'px'
  } else {
    formColumn.value = 2
    formStyle.width = dynamicW / 2 + 'px'
  }
}

/** 根据图片的的宽度或高度,计算图片的高度或宽度,减去dialog中的40像素padding */
function getCalcSize(w, h) {
  const dynamicW = dynamic.dialogWidth - 60
  if (w > h) {
    displayStatus.flexDirection = 'column ',
      dynamic.dynamicWidth = dynamicW
  } else {
    displayStatus.flexDirection = 'row',
      dynamic.dynamicWidth = dynamicW / 2.8
  }
  dynamic.dynamicHeight = dynamic.dynamicWidth * (h / w)
}

/** 获取审核、详情、编辑对话框动态内容 */
async function getFormData(row) {
  await transportInfo(row.originatingId)
}

/** 打开审核、详情、编辑对话框 */
async function viewData(row, type) {
  // 打开时重置表单
  reset()
  await getFormData(row, type)
  row['weightBillImgUrl'] = dialogPreviewUrl.value
  await getDyStyle(row)
  dialogVisiable.value = true;
};

/** 查询运单详情 */
async function transportInfo(id) {
  await transportInfoApi(id).then(response => {
    if (response.code === 1) {
      formView.value = response.data
      dialogPreviewUrl.value = response.data.weightBillImgUrl
    } else {
      ElMessage({ message: response.message, type: 'error' })
    }
  }).catch(err => {
  })
}

/** 重置数据以及表单校验规则  */
async function reset() {
  formView.value = {
    plateNo: undefined,
    driverName: undefined,
    routeName: undefined,
    cargo: undefined,
    departureTime: undefined,
    arrivalTime: undefined,
    weightBillNo: undefined,
    containerNo: undefined,
    checked: undefined,
    checkBy: undefined,
    checkTime: undefined,
    checkNotes: undefined,
  }
}

defineExpose({
  viewData
})
</script>
<style scoped lang='scss'>
@import '../../assets/styles/variables.module.scss';

 /** 动态对话框样式 */
 .dy-dialog {
    grid-gap: 20px;

    :deep(.el-form-item__label) {
      height: 36px;
      text-align: center !important;
      line-height: 36px !important;
    }

    .dy-form {
      flex: 1 !important;

      :deep(.form) {
        width: 100% !important;
      }
    }

    .dy-footer {
      width: 100%;
      display: flex;
      justify-content: flex-end;
      align-items: center;
    }

    .el-image-viewer__wrapper {
      right: 20%;
      left: 20%;
      height: auto;
      width: 60% !important;
      height: auto;
    }

    .el-image-viewer__mask {
      opacity: 0 !important;
    }

    .form-items {
      align-items: center !important;

      .form-input {
        height: 36px !important;
        width: 100%;
      }
    }

    .el-form-item--default {
      margin-bottom: 22px !important;
    }

    .model-value {
      display: inline-block;
      width: 100%;
      color: $--color-info;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }

    .model-remark {
      line-height: 1.5;
      word-wrap: break-word !important;
      white-space: normal !important;
      display: -webkit-box !important;
      -webkit-box-orient: vertical !important;
      /* 子元素排列垂直排列 */
      -webkit-line-clamp: 5 !important;
      /* 设置从第几行后开始省略 */
      overflow: hidden;
    }

    // .no-model-value {
    //     font-size: 20px;
    // }
  }

  /** 表格图片展示样式 */
  .el-image {
    background-color: $--color-bg-info !important;
    color: $--color-info !important;

    .image-slot {
      width: 100%;
      height: 100%;
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 14px;
      z-index: 2000;
    }
  }
</style>